package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.dao.WorkOvertimeConfigMapper;
import com.eastfair.venueservice.dto.WorkOvertimeConfigDTO;
import com.eastfair.venueservice.entity.WorkOvertimeConfig;
import com.eastfair.venueservice.exceptioncode.VenueServiceExceptionCode;
import com.eastfair.venueservice.service.WorkOvertimeConfigService;
import com.eastfair.venueservice.vo.WorkOvertimeConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 加班规则配置
 * </p>
 *
 * @author dq
 * @date 2022-10-19
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class WorkOvertimeConfigServiceImpl extends SuperServiceImpl<WorkOvertimeConfigMapper, WorkOvertimeConfig> implements WorkOvertimeConfigService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<WorkOvertimeConfig> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(WorkOvertimeConfig model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<WorkOvertimeConfig> listWorkOvertimeConfig(WorkOvertimeConfigDTO model) {
        QueryWrapper<WorkOvertimeConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(WorkOvertimeConfig::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(model.getExhibitionManageId() != null, WorkOvertimeConfig::getExhibitionManageId, model.getExhibitionManageId())
        ;
        return list(queryWrapper);
    }

    @Override
    public WorkOvertimeConfigVO getVOByExhibitionManageId(Long exhibitionManageId) {
        WorkOvertimeConfig workOvertimeConfig = getByExhibitionManageId(exhibitionManageId);
        if (workOvertimeConfig == null) {
            return null;
        }
        return BeanUtil.toBean(workOvertimeConfig, WorkOvertimeConfigVO.class);
    }

    @Override
    public WorkOvertimeConfig getByExhibitionManageId(Long exhibitionManageId) {
        WorkOvertimeConfigDTO model = new WorkOvertimeConfigDTO();
        model.setExhibitionManageId(exhibitionManageId);
        List<WorkOvertimeConfig> list = listWorkOvertimeConfig(model);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkOvertimeConfig saveWorkOvertimeConfig(WorkOvertimeConfigDTO workOvertimeConfigDTO) {
        log.info("saveWorkOvertimeConfig - 保存加班规则配置, workOvertimeConfigDTO={}", workOvertimeConfigDTO);
        WorkOvertimeConfig existWorkOvertimeConfig = getByExhibitionManageId(workOvertimeConfigDTO.getExhibitionManageId());
        if (existWorkOvertimeConfig != null) {
            throw BizException.wrap(VenueServiceExceptionCode.WORK_OVERTIME_CONFIG_HAS_EXIST.getCode(), "新增失败，展会已配置加班规则");
        }
        WorkOvertimeConfig workOvertimeConfig = BeanUtil.toBean(workOvertimeConfigDTO, WorkOvertimeConfig.class);
        save(workOvertimeConfig);
        return workOvertimeConfig;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkOvertimeConfig updateWorkOvertimeConfig(WorkOvertimeConfigDTO workOvertimeConfigDTO) {
        log.info("updateWorkOvertimeConfig - 更新加班规则配置, workOvertimeConfigDTO={}", workOvertimeConfigDTO);
        WorkOvertimeConfig workOvertimeConfig = BeanUtil.toBean(workOvertimeConfigDTO, WorkOvertimeConfig.class);
        updateById(workOvertimeConfig);
        return workOvertimeConfig;
    }
}
